<template>
  <Table
    ref="tableRef"
    :url="URL_GETPAGEALAERMSETTING"
    :columns="data.columns"
    :handles="data.handles"
    :query="data.query"
    :toolbar="data.toolbar"
  ></Table>
</template>

<script lang="ts" setup name="alarmSettingList">
import Table from '@/components/table/index.vue';
import { queryDicByParent } from '@/service/apis/api';
import {
  URL_GETPAGEALAERMSETTING,
  deleteAlarmSetting,
} from '@/service/apis/alarm';

const router = useRouter();

const tableRef = ref();

const emit = defineEmits(['get-update']);
onActivated(() => {
  emit('get-update', (flag: boolean) => {
    if (flag) {
      tableRef.value?.getData();
    }
  });
});

const data = reactive({
  columns: [
    {
      prop: 'alarmSettingName',
      label: '告警名称',
    },
    {
      prop: 'alarmSettingType',
      label: '告警类型',
      dic: [] as any,
      type: Number,
    },
    {
      prop: 'updateBy',
      label: '修改人',
    },
    {
      prop: 'updateTime',
      label: '修改时间',
    },
  ],
  toolbar: [
    {
      label: '新增',
      permission: 'alarmSettingList.add',
      icon: 'icon-add',
      type: 'primary',
      handle: (row: any) => {
        router.push({
          name: 'alarmSettingAdd',
        });
      },
    },
  ],
  query: [
    {
      label: '告警名称',
      prop: 'alarmSettingName',
      comName: 'input',
      props: {
        placeholder: '请输入告警名称',
      },
    },
    {
      label: '告警类型',
      prop: 'alarmSettingType',
      comName: 'select',
      props: {
        placeholder: '请选择告警类型',
        options: [] as any,
      },
    },
  ],
  handles: [
    {
      label: '详情',
      permission: 'alarmSettingList.see',
      handle: (row: any) => {
        router.push({
          name: 'alarmSettingDetail',
          query: row,
        });
      },
    },
    {
      label: '修改',
      permission: 'alarmSettingList.edit',
      disabledFun: (row: any) => {
        const accountCode = window.localStorage.getItem('accountCode');
        return (
          (row.alarmSettingType == 1 || row.alarmSettingType == 2) &&
          row.updateBy !== accountCode
        );
      },
      handle: (row: any) => {
        router.push({
          name: 'alarmSettingEdit',
          query: row,
        });
      },
    },
    {
      label: '删除',
      color: '#FA3939',
      disabledFun: (row: any) => {
        const accountCode = window.localStorage.getItem('accountCode');
        return (
          row.alarmSettingType === 3 ||
          row.alarmSettingType === 4 ||
          ((row.alarmSettingType == 1 || row.alarmSettingType == 2) &&
            row.updateBy !== accountCode)
        );
      },
      permission: 'alarmSettingList.delete',
      handle: (row: any) => {
        ElMessageBox.confirm('此操作将永久删除该数据，是否继续？', '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          deleteAlarmSetting({
            id: row.id,
          }).then((res: any) => {
            tableRef.value.getData();
          });
        });
      },
    },
  ],
});

// 字典
queryDicByParent({
  listDtos: ['AlarmSettingType'],
}).then((res: any) => {
  if (res) {
    data.query[1].props.options = [...res.AlarmSettingType];
    data.columns[1].dic = [...res.AlarmSettingType];
  }
});
</script>